document.addEventListener('DOMContentLoaded', function() {
    // 清空 sessionStorage
    sessionStorage.clear();
    const loginForm = document.getElementById('loginForm');
    const registerBtn = document.getElementById('registerBtn');

    loginForm.addEventListener('submit', function(e) {
        e.preventDefault();
        
        const workId = document.getElementById('workId').value;
        const password = document.getElementById('password').value;

        fetch('http://localhost:8888/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                uid: workId,
                upassword: password
            })
        })
        .then(response => response.json())
        .then(data => {
            if(data.result) {
                alert('登录成功!');
                // 使用 sessionStorage 存储用户信息, 用于后续的页面访问
                sessionStorage.setItem('userInfo', JSON.stringify(data.userinfo));
                window.location.href = 'main.html';
            } else {
                alert('登录失败,请检查工号和密码!');
            }
        })
        .catch(error => {
            console.error('登录错误:', error);
            alert('登录出错,请稍后重试!');
        });
    });

    registerBtn.addEventListener('click', function() {
        window.location.href = 'register.html';
    });
}); 